package com.itheima.project.face.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.adapter.SmsTemplateAdapter;
import com.itheima.project.constant.sms.SmsCacheConstant;
import com.itheima.project.core.enums.sms.SmsTemplateEnum;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.face.SmsTemplateFace;
import com.itheima.project.pojo.SmsTemplate;
import com.itheima.project.service.ISmsTemplateService;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.utils.EmptyUtil;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.vo.basic.OtherConfigVo;
import com.itheima.project.vo.sms.SmsTemplateVo;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SmsTemplateFaceImpl.java
 * @Description TODO
 */
@Slf4j
@Component
public class SmsTemplateFaceImpl implements SmsTemplateFace {

    @Autowired
    SmsTemplateAdapter smsTemplateAdapter;

    @Autowired
    ISmsTemplateService smsTemplateService;

    @Override
    @Cacheable(value = SmsCacheConstant.PAGE_TEMPLATE,key ="#pageNum+'-'+#pageSize+'-'+#smsTemplateVo.hashCode()")
    public Page<SmsTemplateVo> findSmsTemplateVoPage(SmsTemplateVo smsTemplateVo, int pageNum, int pageSize) {
        try {
            //查询Page<SmsTemplateVo>图片分页
            Page<SmsTemplate> page = smsTemplateService.findSmsTemplateVoPage(smsTemplateVo,pageNum,pageSize);
            //转化Page<SmsTemplateVo>为Page<SmsTemplateVo>
            Page<SmsTemplateVo> pageVo = BeanConv.toPage(page,SmsTemplateVo.class);
            List<SmsTemplateVo> smsTemplateVoList = pageVo.getRecords();
            if (!EmptyUtil.isNullOrEmpty(smsTemplateVoList)){
                smsTemplateVoList.forEach(n->{
                    List <OtherConfigVo> list = JSONArray.parseArray(n.getOtherConfig(), OtherConfigVo.class);
                    n.setOtherConfigs(list);
                });
            }
            //返回结果
            return pageVo;
        } catch (Exception e) {
            log.error("查询签名列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.PAGE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_TEMPLATE,allEntries = true)},
            put={@CachePut(value =SmsCacheConstant.PREFIX_TEMPLATE,key = "#result.id")})
    public SmsTemplateVo addSmsTemplate(SmsTemplateVo smsTemplateVo) throws ProjectException {
        try {
            return smsTemplateAdapter.addSmsTemplate(smsTemplateVo);
        } catch (Exception e) {
            log.error("添加签名异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.CREATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_TEMPLATE,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.PREFIX_TEMPLATE,allEntries = true)})
    public Boolean deleteSmsTemplate(String[] checkedIds) throws ProjectException {
        try {
            return smsTemplateAdapter.deleteSmsTemplate(checkedIds);
        } catch (Exception e) {
            log.error("删除签名异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.DELETE_FAIL);
        }
    }

    @Override
    @Transactional
    @Caching(evict = {@CacheEvict(value = SmsCacheConstant.PAGE_TEMPLATE,allEntries = true),
            @CacheEvict(value = SmsCacheConstant.PREFIX_TEMPLATE,key = "#smsTemplateVo.id")})
    public SmsTemplateVo modifySmsTemplate(SmsTemplateVo smsTemplateVo) throws ProjectException {
        try {
            return smsTemplateAdapter.modifySmsTemplate(smsTemplateVo);
        } catch (Exception e) {
            log.error("修改签名异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.UPDATE_FAIL);
        }
    }

    @Override
    @Transactional
    @Cacheable(value =SmsCacheConstant.PREFIX_TEMPLATE,key = "#smsTemplateVo.id")
    public SmsTemplateVo querySmsTemplate(SmsTemplateVo smsTemplateVo) throws ProjectException {
        try {
            return smsTemplateAdapter.querySmsTemplate(smsTemplateVo);
        } catch (Exception e) {
            log.error("查询签名状态异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.SELECT_FAIL);
        }
    }

    @Override
    @Transactional
    public SmsTemplateVo updateSmsTemplateEnableFlag(SmsTemplateVo smsTemplateVo) {
        try {
            SmsTemplate smsTemplate = BeanConv.toBean(smsTemplateVo, SmsTemplate.class);
            boolean flag = smsTemplateService.updateById(smsTemplate);
            if (flag){
                return smsTemplateVo;
            }
        } catch (Exception e) {
            log.error("修改签名状态异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(SmsTemplateEnum.UPDATE_FAIL);
        }
        return null;
    }
}
